package com.zhengtoon.mctoon.conv.utils;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Date;

/**
 * 日期转换  144637
 */
public class DateFormatAppUtils {

    /**
     * 获取默认日期格式 144637
     *
     * @param timestamp
     * @return
     */
    public static String getFormatDate(Long timestamp) {
        String formatDate = DateFormatUtils.format(timestamp, AppConstant.DEFORT_TIME_FORMAT_PATTERN);
        return formatDate;
    }

    /**
     * 获取5分钟前的时间戳   144637
     *
     * @param minutes
     * @return
     */
    public static Long getTimeBeforeMinutes(Integer minutes) {
        Date current = new Date();
        Date dateBeforeFiveMinutes = DateUtils.addMinutes(current, minutes);
        return dateBeforeFiveMinutes.getTime();
    }

    //        1分钟之内的显示：刚刚
//        1小时之内的显示：分钟数+分钟前
//        今天的显示：小时数+小时前
//        昨天发的显示：昨天
//        超过昨天的显示：日期
    public static String formatDateTime(Long date) {


        Long expiredDays = pastDays(date);
        if (expiredDays == 1) {
            return "昨天";
        }
        if (expiredDays > 1) {

            return formatDate(date);
        }
        if (expiredDays == 0) {
            Long expiredHours = pastHour(date);
            if (expiredHours >= 1) {
                return expiredHours + "小时前";
            }
            if (expiredHours < 1) {
                Long expiredMinutes = pastMinutes(date);
                if (expiredMinutes >= 1) {
                    return expiredMinutes + "分钟前";
                }
                if (expiredMinutes < 1) {
                    return "刚刚";
                }
            }
        }
        return null;
    }

    /**
     * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String formatDate(Long date) {
        return DateFormatUtils.format(date, "yyyy-MM-dd");
    }


    /**
     * 获取过去的天数
     *
     * @param date
     * @return
     */
    public static long pastDays(Long date) {
        return new Date().getTime() / (24 * 60 * 60 * 1000) - date / (24 * 60 * 60 * 1000);
    }

    /**
     * 获取过去的小时
     *
     * @param date
     * @return
     */
    public static long pastHour(Long date) {
        return (new Date().getTime() - date) / (60 * 60 * 1000);
    }

    /**
     * 获取过去的分钟
     *
     * @param date
     * @return
     */
    public static long pastMinutes(Long date) {
        return (new Date().getTime() - date) / (60 * 1000);
    }

    public static void main(String[] args) {
//        System.out.println(getTimeBeforeMinutes(-5));

        System.out.println(formatDateTime(1496490866618L));

        System.out.println((new Date().getTime() / (24 * 60 * 60 * 1000)) - (1496490866618L / (24 * 60 * 60 * 1000)));
    }
}
